package edu.spbu.eclipse.addressbook.andrpodk;

import org.eclipse.swt.events.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.*;

public class ChangeEntryDialog extends SelectionAdapter implements KeyListener{
	private Shell dialogShell;
	private Text name,telephone,e_mail,address;
	private AddressBookEditor dataHandler;
	private Entry entry;

	public ChangeEntryDialog(Shell parentShell,AddressBookEditor dataHandler,Entry entry){
		this.dataHandler=dataHandler;
		this.entry=entry;
		dialogShell=new Shell(parentShell,SWT.DIALOG_TRIM|SWT.APPLICATION_MODAL);
		dialogShell.setText("Change Entry");
		dialogShell.setLocation(200, 200);
		dialogShell.open();
		dialogShell.setSize(200, 280);

		Label nameLabel=new Label(dialogShell,SWT.NONE);
		nameLabel.setText("Name:");
		nameLabel.setSize(100, 20);
		nameLabel.setLocation(20, 20);
		name=new Text(dialogShell,SWT.SINGLE|SWT.BORDER);
		name.setBounds(20, 40, 150, 20);
		name.setText(entry.getName());
		name.setFocus();
		name.selectAll();
		name.addKeyListener(this);

		Label telephoneLabel=new Label(dialogShell,SWT.NONE);
		telephoneLabel.setText("Telephone:");
		telephoneLabel.setBounds(20, 63, 100, 20);
		telephone=new Text(dialogShell,SWT.SINGLE|SWT.BORDER);
		telephone.setBounds(20, 83, 150, 20);
		telephone.setText(entry.getTelephone());
		telephone.selectAll();
		telephone.addKeyListener(this);

		Label e_mailLabel=new Label(dialogShell,SWT.NONE);
		e_mailLabel.setText("E_Mail:");
		e_mailLabel.setBounds(20, 106, 100, 20);
		e_mail=new Text(dialogShell,SWT.SINGLE|SWT.BORDER);
		e_mail.setBounds(20, 126, 150, 20);
		e_mail.setText(entry.getE_mail());
		e_mail.selectAll();
		e_mail.addKeyListener(this);

		Label addressLabel=new Label(dialogShell,SWT.NONE);
		addressLabel.setText("Address:");
		addressLabel.setBounds(20, 149, 100, 20);
		address=new Text(dialogShell,SWT.SINGLE|SWT.BORDER);
		address.setBounds(20, 169, 150, 20);
		address.setText(entry.getAddress());
		address.selectAll();
		address.addKeyListener(this);

		Button ok=new Button(dialogShell,SWT.PUSH);
		ok.setText("OK");
		ok.setBounds(80, 210, 35, 25);
		ok.addSelectionListener(this);
		ok.addKeyListener(this);
	}

	public void widgetSelected(SelectionEvent e){
		if(!name.getText().equals("")){									
			dataHandler.handleChangeEntryData(entry,new Entry(name.getText(),telephone.getText(),
					e_mail.getText(),address.getText()));
			dialogShell.dispose();
		}else{
			name.setFocus();
		}
	}

	public void keyReleased(KeyEvent e) {}

	public void keyPressed(KeyEvent e) {
		if(e.character==SWT.CR){
			widgetSelected(null);			
		}		
	}
}
